package com.c5c7.servlet;

import com.c5c7.bean.AdminBean;
import com.c5c7.dao.AdminDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;


@WebServlet("/admin.jsp")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String p = req.getParameter("page");
        int page = 1;
        if (p != null && p.equals("")) {
            page = Integer.parseInt(p);
        }
        AdminDao adminDao = new AdminDao();
        List<AdminBean> admin = adminDao.getList(page);
        int totalCount,curPage,totalPage,upPage,downPage;
        totalCount = adminDao.count();
        curPage = page;
        if (totalCount%10 == 0) {
            totalPage = totalCount/10;
        }else {
            totalPage = totalCount / 10 + 1;
        }
        if (curPage == 1) {
            upPage = 1;
        } else {
            upPage = curPage - 1;
        }
        if (curPage == totalPage) {
            downPage = totalPage;
        }else {
            downPage = curPage + 1;
        }
        req.setAttribute("totalPage", totalPage);
        req.setAttribute("curPage", curPage);
        req.setAttribute("totalCount", totalCount);
        req.setAttribute("upPage", upPage);
        req.setAttribute("downPage", downPage);
        req.setAttribute("admin", admin);
        req.getRequestDispatcher("adminUser.jsp").forward(req, resp);
    }
}
